#include "version_shader.hpp"

#include "OpenLog.h"
#include "util/Util.hpp"
#include <math.h>

#define numVAOs 1

namespace
{
    constexpr double PI = 3.14159265358979323846;

    class Main_shader : public Interface_app
    {
    private:
        std::shared_ptr<Base_WindowCreate> m_window;
        GLuint m_renderingProgram;
        const static float MOVE_SIDE;
        GLuint m_vao[numVAOs];
        float m_x;
        float m_y;

    public:
        Main_shader();
        virtual ~Main_shader();
        virtual void init(std::shared_ptr<Base_WindowCreate> m_baseWindow) override;
        virtual void onRender() override;
    };

    const float Main_shader::MOVE_SIDE = 5.0f;

} // namespace

int main_01_shader(int argc, char const *argv[])
{
    std::shared_ptr<Base_WindowCreate> window = std::make_shared<Base_WindowCreate>();
    std::shared_ptr<Main_shader> app = std::make_shared<Main_shader>();
    app->init(window);
    app->onRender();

    return 0;
}

namespace
{
    Main_shader::Main_shader()
    {
    }

    Main_shader::~Main_shader()
    {
    }

    void Main_shader::init(std::shared_ptr<Base_WindowCreate> m_baseWindow)
    {
        LOGI("");
        m_renderingProgram = openutil::createShaderProgram("vertShader_1.glsl", "fragShader_1.glsl");
        glGenVertexArrays(numVAOs, m_vao);
        glBindVertexArray(m_vao[0]);

        m_window = m_baseWindow;
        m_x = 0.5f;
        m_y = 0.5f;

        // double theta = 45 * PI / 180.0; // 角度转弧度

        // double cosTheta = cos(theta);
        // double sinTheta = sin(theta);
        // double newX = m_x * cosTheta + m_y * sinTheta;
        // double newY = -m_x * sinTheta + m_y * cosTheta;

        // m_x = newX;
        // m_y = newY;
    }

    void Main_shader::onRender()
    {
        while (!m_window->windowShouldClose())
        {
            /* 设置颜色 */
            glClear(GL_DEPTH_BUFFER_BIT);
            glClearColor(0.0, 0.0, 0.0, 1.0);
            glClear(GL_COLOR_BUFFER_BIT); /* 背景清除为黑色 */

            glUseProgram(m_renderingProgram);

            // GLuint offsetLoc = glGetUniformLocation(m_renderingProgram, "offset"); /* 获取offset指针 */
            // glProgramUniform1f(m_renderingProgram, offsetLoc, m_x);                /* 将x传入到offset(在顶点着色器)中 */

            GLuint xLoc = glGetUniformLocation(m_renderingProgram, "eq_x");
            GLuint yLoc = glGetUniformLocation(m_renderingProgram, "eq_y");

            glProgramUniform1f(m_renderingProgram, xLoc, m_x);
            glProgramUniform1f(m_renderingProgram, yLoc, m_y);

            glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

            glfwSwapBuffers(m_window->getWindow().get());
            glfwPollEvents();

            double theta = MOVE_SIDE * PI / 180.0; // 角度转弧度

            double cosTheta = cos(theta);
            double sinTheta = sin(theta);
            double newX = m_x * cosTheta + m_y * sinTheta;
            double newY = -m_x * sinTheta + m_y * cosTheta;

            m_x = newX;
            m_y = newY;
            LOGI("x: %f, y: %f", m_x, m_y);

            openutil::checkOpenGLError();
        }
    }

} // namespace